<?php if(get('ini_post')){
	$post_num = get('ini_postnum');
	$tmp_post = $post;
	$tags = ''; $i = 0;
	if ( get('ini_post_title')=="文章标题" ){
		$n = 1;
	}
	elseif ( get('ini_post_title')=="预览内容" ){
		$n = 2; 
	}
	else {
		$n = 0;
	}
	echo get('ini_post_thumb')=='显示'? "<div class=\"related-post\">": "<section class=\"post-meta\"><ul>";
	if ( get_the_tags( $post->ID ) ){
		foreach ( get_the_tags($post->ID) as $tag )
		$tags.= $tag->name.',';
		$tags = strtr(rtrim($tags, ','), ' ', '-');
		$myposts = get_posts( 'numberposts='.$post_num.'&tag='.$tags.'&exclude='.$post->ID );
		foreach( $myposts as $post ){
			setup_postdata($post);
			echo post_list( $n );
			$i ++;
		}
	}
	if ( $i < $post_num ){
		$post = $tmp_post; setup_postdata($post);
		$cats = '';
		$post_num -= $i;
		foreach ( get_the_category( $post->ID ) as $cat )
		$cats.= $cat->cat_ID.',';
		$cats = strtr(rtrim($cats, ','), ' ', '-');
		$myposts = get_posts( 'numberposts='.$post_num.'&category='.$cats.'&exclude='.$post->ID );
		foreach( $myposts as $post ){
			setup_postdata($post);
			echo post_list( $n );
			$i ++;
		}
	}
	if ( $i < $post_num ){
		$post_num -= $i;
		$myposts = get_posts( 'numberposts='.$post_num.'&orderby=rand&exclude='.$post->ID );
		foreach( $myposts as $post ){
			setup_postdata($post);
			echo post_list( $n );
		}
	}
	$post = $tmp_post;
	setup_postdata($post);
echo "<div class=\"clear\"></div>";
echo get('ini_post_thumb')=='显示'? "</div>": "<div class=\"m-t\">相关文章</div></ul></section>";
}

function post_list( $n ){
	global $post;
	if( get('ini_post_thumb')=='显示' ){
		?><a href="<?php the_permalink() ?>" <?php if (0!=$n){ echo 'title="'; if ($n==1){ the_title(); } elseif ($n==2){ ini_excerpt(120); } echo '"'; }?>><?php echo ini_img(50,50)?><span><?php the_title()?></span></a><?php
	}
	else {
		?><li><a href="<?php the_permalink() ?>" <?php if (0!=$n){ echo 'title="'; if ($n==1){ the_title(); } elseif ($n==2){ ini_excerpt(120); } echo '"'; }?>><?php the_title()?></a></li>
<?php
	}
}
?>
